// pages/yimiaodetail/index.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: ['08:00-11:30', '14:00-17:30' ],
    contentshijian:'',
    date: '',
    show: false,
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    Toast(`当前值：${value}, 当前索引：${index}`);
  },
  //新闻选择
  onChangexinwen(event) {
    this.setData({
      shijiansuan: event.detail.value,

      xinwenpd: false,
      showa: false
    });
  },
  
  oncancelxinwen() {
    this.setData({
      xinwenpd: false,
      showa: false
    })
  },
  chosexinwen() {
    this.setData({
      xinwenpd: true,
      showa: true
    })
  },


  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {

    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
      showa:true
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    db.collection('yimiao').doc(options.id).get().then(res => {
      console.log(res);
      this.setData({
        type:wx.getStorageSync('user').xinwentype,
        list: res.data,
        id: options.id
      })
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  yuyue() {
    let that = this;
    if(that.data.date)
{    wx.showModal({
      title: '温馨提示',
      content: '是否预约？',
      confirmText: "是",
      cancelText: "否",
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '预约中'
          })
          db.collection('dingdan').add({
            data: {
              user:wx.getStorageSync('user'),
              yiyuanuserid:that.data.list.yiyuanuserid,
              date:that.data.date,
              shijiansuan:that.data.shijiansuan,
              yiyuanid: that.data.id,
              list: that.data.list,
              zhanghao:wx.getStorageSync('user').zhanghao,
              mima:wx.getStorageSync('user').mima,
            }
          }).then(res => {
            console.log(res);
            wx.redirectTo({
              url: '../dingdandetail/index?id='+res._id,
            })
            wx.hideLoading({
              success: (res) => {
             
              },
            })
          })

          // wx.cloud.callFunction({
          //   name: "update",
          //   data: {
          //     action: "pushxmid",
          //     id: that.data.id,
          //     bmuser: {
          //       phonenumber: that.data.phonenumber,
          //       name: that.data.bmname
          //     }
          //   }
          // }).then(res => {


          //   console.log(res);
          //   wx.hideLoading({
          //     success: (res) => {},
          //   })
          //   if (res.result.data.stats.updated === 1) {

          //     wx.showToast({
          //       title: '成功',
          //     })

          //   }
          // })

        } else if (res.cancel) {

        }
      }
    })}
    else{
      wx.showToast({
        title: '请选择预约时间',
        icon:'none'
      })
    }
  },
  shanchudingdan(e){
    let that = this;

{    wx.showModal({
      title: '温馨提示',
      content: '是否删除？',
      confirmText: "是",
      cancelText: "否",
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中'
          })
          db.collection('yimiao').doc(that.data.id).remove()   
          .then(res => {
            console.log(res);
             wx.navigateBack({
               delta: 1,
             })
            wx.hideLoading({
              success: (res) => {
             
              },
            })
          })

        } else if (res.cancel) {

        }
      }
    })}

  }

})